export const Base = {
    data () {
        return {
           
            operateModal:false,
            list: [],
            filter:{
                total:0,
                rows:10,
                page:1,
                name:''
            },
            ids:[],
            id:0,
            module:'users',
            
            
        }
    },
    methods: {
        
         
        handleReset (name) {
            this.$refs[name].resetFields();
        },
        
        remove (params) {
           

            this.$Modal.confirm({
                title: '确认删除数据？',
                content: '<p>你确认删除该条数据吗</p>',
                onOk: () => {
                    this.axios({
                        url:`http://localhost:3000/${this.module}/data/${params.row._id}`,
                        method:'delete'
                    }).then(res=>{
                        this.$Message.info('删除数据成功')
                        this.getlistData()
                    })
                },
                onCancel: () => {
                    this.$Message.info('取消删除');
                }
            });
        },
        getlistData(){
            this.axios({
            url:`http://localhost:3000/${this.module}/list`,
            method:'post',
            data:{
                rows:this.filter.rows,
                page:this.filter.page,
                name:this.filter.name
            }
        }).then(res=>{
            this.list=res.data.rows;
            this.filter.total=res.data.total;
            
        })
        },
        onPagechange(page){
            this.filter.page=page;
            this.getlistData()
        },
        onPagesizechange(rows){
            this.filter.rows=rows;
            this.getlistData()
        },
        onSelectionChange(selection){
            var ids=[]
            for(let i=0;i<selection.length;i++){
                ids.push(selection[i]._id)
            }
            this.ids=ids
        },
        removes(){
           
           this.$Modal.confirm({
                title: '确认删除数据？',
                content: '<p>你确认删除该条数据吗</p>',
                onOk: () => {
                    

                    this.axios({
                        url:`http://localhost:3000/${this.module}/removes`,
                        method:'post',
                        data:{
                            ids:this.ids.toString()
                        }
                    }).then(res=>{
                        this.$Message.info('删除数据成功')
                        this.getlistData()
                    })
                },
                onCancel: () => {
                    this.$Message.info('取消删除');
                }
            });
        },
        addDate(){
            this.formValidate={}
           
            this.operateModal=true
        },
        editDate(params){
           
            this.axios({
                url:`http://localhost:3000/${this.module}/data/${params.row._id}`,
                method:'get'
            }).then(res=>{
                console.log(res)
                this.formValidate=res.data;
                this.operateModal=true;
                this.id=params.row._id;
                 
            })
        },
        plData(params){
            console.log(params)
        },
        handleSubmit (name) {
           
            this.$refs[name].validate((valid) => {
                if (valid) {

                    if(this.id!=0){
                        this.axios({
                            url:`http://localhost:3000/${this.module}/data/${this.id}`,
                            method:'put',
                            data:this.formValidate
                        }).then(res=>{
                        this.$Message.success('数据操作成功')
                        this.operateModal=false;
                        this.getlistData()
                        this.id=0
                        this.$refs[name].resetFields();
                        })
                    }else{
                        console.log(this.id)
                        
                        this.axios({
                        url:`http://localhost:3000/${this.module}/data`,
                        method:'post',
                        data:this.formValidate
                    }).then(res=>{
                        this.$Message.success('数据操作成功')
                        this.operateModal=false;
                        this.getlistData()
                        this.$refs[name].resetFields();
                        
                    })  
                    }
                   
                } else {
                    this.$Message.error('Fail!');
                }
            })
        },
        cancel () {
            this.$Message.info('Clicked cancel');
            this.id=0
        },
        search(){
            this.filter.page=1
            this.getlistData()
        }
    },
    mounted(){
       this.getlistData()
    }
}
